package com.qf.zyh.web.controller.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.zyh.common.Const;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.admin.AdminSer;
import com.qf.zyh.services.admin.Impl.AdminSerImpl;
import com.qf.zyh.util.StrUtils;
import com.qf.zyh.web.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/adminUser")
public class AdminUserCout extends BaseServlet {
AdminSer as = new AdminSerImpl();
    public String login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //登录要验证是否是管理员账户，是管理员账户则登录进入
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user =as.adminLogin(username,password);
        if (user!=null) {
            request.setAttribute(Const.UserCont.ADMIN_LOGIN,user);
            return Const.SysConst.REDIRECT+"admin/admin.jsp";
        }else {
            request.setAttribute("msg","用户名密码有误，或不是管理员");
            return Const.SysConst.FORWARD+"login.jsp";
        }
    }
    //获取所有用户数据
    public String getUserList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        List<User> userList = as.seleall();
        //转成json数据
        ObjectMapper om = new ObjectMapper();
        String s = om.writeValueAsString(userList);
        return s;
    }
    //条件展示用户
    public String getUserListTwo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");

        List<User> userList = as.selein(username,gender);
        ObjectMapper om = new ObjectMapper();
        String s = om.writeValueAsString(userList);
        return s;
    }
    //删除用户
    public Boolean deleteUser(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        Integer uid = StrUtils.toint(request.getParameter("uid"));
        Boolean a = as.deleUser(uid);
        return a;
    }

}
